package com.template.app.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;

public class Response implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	
	public static final String NAME = "response";

	public static final int SUCCESS = 0;
	public static final int ERROR = 1;
	public static final int VALIDATION_ERROR = 2;
	public static final int ACCESS_DENIED = 3;
	public static final int CONFIRMATION = 4;
	public static final int BAD_CREDENTIALS = 5;
	
	private Integer type;
	private Map<String, List<String>> errorsMap;
	
	public Response() {}
	
	public Response(Integer type) {
		this.type = type;
	}
	
	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}	

	@JsonProperty("errors")
	public Map<String, List<String>> getErrorsMap() {
		return errorsMap;
	}

	public void setErrorsMap(Map<String, List<String>> errorsMap) {
		this.errorsMap = errorsMap;
	}

	public void addError(String name, String error) {
		if (errorsMap == null) {
			errorsMap = new LinkedHashMap<String, List<String>>();
		}
		
		if (errorsMap.get(name) == null) {
			errorsMap.put(name, new ArrayList<String>());
		}		
		errorsMap.get(name).add(error);
	}
	
	public boolean hasErrors(String name) {
		return errorsMap != null && errorsMap.containsKey(name);
	}
	
	public boolean hasErrors() {
		return errorsMap != null && !errorsMap.isEmpty();
	}
	
	public List<String> getErrors(String name) {
		return errorsMap == null ? new ArrayList<String>() : errorsMap.get(name);
	}
	
	@JsonIgnore
	public List<String> getErrors() {
		List<String> errors = new ArrayList<String>();
		if (errorsMap == null) {
			return errors;
		}
		
		for (String name : errorsMap.keySet()) {
			errors.addAll(errorsMap.get(name));
		}
		
		return errors;
	}

}
